package com.example.g7640.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.g7640.dto.ProductDto;
import com.example.g7640.dto.ProductQuery;
import com.example.g7640.dto.common.PageResult;
import com.example.g7640.entity.Product;
import com.example.g7640.exception.BizException;
import com.example.g7640.mapper.ProductMapper;
import com.example.g7640.service.ProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.g7640.util.BaseConvertor;
import com.example.g7640.util.PageUtil;
import com.example.g7640.util.RoleCheckUtil;
import com.example.g7640.util.SessionLocal;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author  
 * @since 2024-03-18
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Resource
    ProductMapper productMapper;

    @Override
    public PageResult<List<ProductDto>> query(ProductQuery query) {
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        if(query.getVid() != null) {
            wrapper.eq(Product::getVid, query.getVid());
        }
        if(query.getName() != null) {
            wrapper.like(Product::getName, query.getName());
        }
        if(query.getTags() != null) {
            wrapper.like(Product::getTags, query.getTags());
        }
        if(query.getStatus() != null) {
            wrapper.eq(Product::getStatus, query.getStatus());
        }
        if(query.getInventory() != null){
            if(query.getInventory()){
                wrapper.gt(Product::getInventory,0);
            }else{
                wrapper.le(Product::getInventory,0);
            }
        }

        if(query.getPriceMin() != null && query.getPriceMax() != null) {
            wrapper.between(Product::getPrice, query.getPriceMin(), query.getPriceMax());
        }
        IPage iPage = productMapper.selectPage(PageUtil.pageToMybatisPage(query), wrapper);
        PageResult<List<ProductDto>> pageResult = PageUtil.mybatisPageToPage(iPage, ProductDto.class);
        return pageResult;
    }

    @Override
    public ProductDto getInfo(Integer id) {
        Product product = productMapper.selectById(id);
        return BaseConvertor.convert(product,ProductDto.class);
    }

    @Override
    public void update(ProductDto productDto) {
        Product productDb = productMapper.selectById(productDto.getId());
        if(productDb == null){
            throw new BizException("product is null");
        }
        productDb.setStatus(productDto.getStatus());
        productDb.setInventory(productDto.getInventory());
        productDb.setName(productDto.getName());
        productDb.setPrice(productDto.getPrice());
        productDb.setTags(productDto.getTags());
        LocalDateTime now = LocalDateTime.now();
        productDb.setUpdatedAt(now);
        productMapper.updateById(productDb);
    }

    @Override
    public void updateStatus(ProductDto productDto) {
        Product product = BaseConvertor.convert(productDto,Product.class);
        Product productDb = productMapper.selectById(productDto.getId());
        if(productDb == null){
            throw new BizException("Product is not exist");
        }
        productDb.setUpdatedAt(LocalDateTime.now());
        productDb.setStatus(productDto.getStatus());
        productMapper.updateById(productDb);
    }

    @Override
    public void add(ProductDto productDto) {
        RoleCheckUtil.checkVendor();
        Product product = BaseConvertor.convert(productDto,Product.class);
        product.setVid(SessionLocal.getLoginId());
        LocalDateTime now = LocalDateTime.now();
        product.setCreatedAt(now);
        product.setUpdatedAt(now);
        productMapper.insert(product);
    }

    @Override
    public void delete(Integer id) {
        Product productDb = productMapper.selectById(id);
        productMapper.deleteById(id);
    }
}
